//---------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
//---------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Data;
using System.Globalization;
using System.Collections.Generic;


namespace PlanetWPF.Toolbelt.Controls
{
    /// <summary>
    /// DayHeaderConverter converts to FirstDayOfWeek and offset to the correct DayOfWeek value
    /// </summary>
    public sealed class DayHeaderConverter : IValueConverter
    {
        /// <summary>
        /// convert FirstDayOfWeek and offset to the correct DayOfWeek value
        /// </summary>
        /// <param name="value">DayOfWeek</param>
        /// <param name="targetType">DayOfWeek</param>
        /// <param name="parameter">0~6, column index of each month</param>
        /// <param name="culture">CultureInfo</param>
        /// <returns>DayOfWeek value</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DayOfWeek)
            {
                int offset = Int32.Parse((string)parameter, CultureInfo.InvariantCulture);
                return (DayOfWeek)(((int)value + offset) % 7);
            }

            return null;
        }

        /// <summary>
        /// Not supported
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    /// <summary>
    /// AbbreviatedDayNameConverter converts the DayOfWeek value to a culture related abbreviated day name string
    /// </summary>
    public sealed class AbbreviatedDayNameConverter : IValueConverter
    {
        /// <summary>
        /// Convert DayOfWeek value to a culture related abbreviated day name string
        /// </summary>
        /// <param name="value">DayOfWeek</param>
        /// <param name="targetType">string</param>
        /// <param name="parameter">null</param>
        /// <param name="culture"></param>
        /// <returns>abbreviated day name string</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DayOfWeek)
            {
                if (culture == null)
                {
                    culture = CultureInfo.CurrentCulture;
                }

                if (culture.DateTimeFormat != null)
                {
                    return culture.DateTimeFormat.AbbreviatedDayNames[(int)value];
                }
            }

            return string.Empty;
        }

        /// <summary>
        /// Not supported
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}
